package com.jingdianjichi.auth.application.controller;

import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.jindianjichi.common.entity.Result;
import com.jingdianjichi.auth.application.convert.AuthUserDTOConverter;
import com.jingdianjichi.auth.application.dto.AuthUserDTO;
import com.jingdianjichi.auth.domain.entity.AuthUserBO;
import com.jingdianjichi.auth.domain.service.AuthUserDomainService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/user/")
@Slf4j
public class UserController {

    @Resource
    private AuthUserDomainService authUserDomainService;


    @PostMapping("registry")
    public Result<Boolean> registry(@RequestBody @Validated AuthUserDTO authUserDTO) {
        try {
            AuthUserBO authUserBO = AuthUserDTOConverter.INSTANCE.convertDTOToBO(authUserDTO);
            Boolean register = authUserDomainService.register(authUserBO);
            //简历初步角色关联
            return Result.ok(register);
        } catch (Exception e) {
            log.error("注册失败:{}", e.getMessage());
            return Result.fail(false);
        }
    }

    @PostMapping("update")
    public Result<Boolean> update(@RequestBody @Validated AuthUserDTO authUserDTO) {
        try {
            AuthUserBO authUserBO = AuthUserDTOConverter.INSTANCE.convertDTOToBO(authUserDTO);
            Boolean register = authUserDomainService.update(authUserBO);
            return Result.ok(register);
        } catch (Exception e) {
            log.error("注册失败:{}", e.getMessage());
            return Result.fail(false);
        }
    }

    @PostMapping("delete")
    public Result<Boolean> delete(@RequestBody AuthUserDTO authUserDTO) {
        try {
            AuthUserBO authUserBO = AuthUserDTOConverter.INSTANCE.convertDTOToBO(authUserDTO);
            Boolean register = authUserDomainService.update(authUserBO);
            return Result.ok(register);
        } catch (Exception e) {
            log.error("注册失败:{}", e.getMessage());
            return Result.fail(false);
        }
    }

    @PostMapping("status")
    public Result<Boolean> changeStatus(@RequestBody AuthUserDTO authUserDTO) {
        try {
            AuthUserBO authUserBO = AuthUserDTOConverter.INSTANCE.convertDTOToBO(authUserDTO);
            Boolean register = authUserDomainService.update(authUserBO);
            return Result.ok(register);
        } catch (Exception e) {
            log.error("注册失败:{}", e.getMessage());
            return Result.fail(false);
        }
    }

    /**
     * 登录
     * @return
     */
    @RequestMapping("doLogin")
    public Result<SaTokenInfo> doLogin(@RequestParam("validCode") String validCode) {
        try{
            log.info("开始登录:{}", validCode);
            SaTokenInfo saTokenInfo =authUserDomainService.doLogin(validCode);
            return Result.ok(saTokenInfo);
        }catch (Exception e){
            return Result.fail();
        }
    }

    /**
     * 登录
     * @return SaResult
     */
    @GetMapping("isLogin")
    public String isLogin() {
        return "当前会话是否登录：" + StpUtil.isLogin();
    }

    /**
     * 获取用户信息
     * @return SaResult
     */
    @PostMapping("getUserInfo")
    public Result<AuthUserDTO> getUserInfo(@RequestBody @Validated AuthUserDTO authUserDTO) {
        try{
            AuthUserBO authUserBO = AuthUserDTOConverter.INSTANCE.convertDTOToBO(authUserDTO);
            AuthUserBO userInfo = authUserDomainService.getUserInfo(authUserBO);
            AuthUserDTO result = AuthUserDTOConverter.INSTANCE.convertBOToDTO(userInfo);
            return Result.ok(result);
        }catch (Exception e){
            return Result.fail();
        }
    }

    @PostMapping("logout")
    public Result<String> logout(@RequestParam("userName") String userName) {
        try{
            if(StringUtils.isBlank(userName)){
                return Result.fail("不能空");
            }
            StpUtil.login(userName);
            return Result.ok("退出成功");
        }catch (Exception e){
            log.error("退出失败:{}", e.getMessage());
            return Result.fail();
        }
    }



}
